package com.huang.core.widget.edittext

import android.text.Editable
import com.huang.core.util.NumberUtil

open class MinTextChangeListener private constructor() : SimpleTextChangeListener() {

    private var dMinCount: Double? = null
    private var fMinCount: Float? = null
    private var lMinCount: Long? = null
    private var iMinCount: Int? = null
    private var sMinCount: Short? = null

    constructor(maxCount: Double) : this() {
        this.dMinCount = maxCount
    }

    constructor(maxCount: Float) : this() {
        this.fMinCount = maxCount
    }

    constructor(maxCount: Long) : this() {
        this.lMinCount = maxCount
    }

    constructor(maxCount: Int) : this() {
        this.iMinCount = maxCount
    }

    constructor(maxCount: Short) : this() {
        this.sMinCount = maxCount
    }


    override fun afterTextChanged(s: Editable) {
        super.afterTextChanged(s)
        if (dMinCount != null) {
            if (NumberUtil.string2Double(s.toString()) < dMinCount!!) {
                s.replace(0, s.length, "$dMinCount")
            }
        } else if (fMinCount != null) {
            if (NumberUtil.string2Float(s.toString()) < fMinCount!!) {
                s.replace(0, s.length, "$fMinCount")
            }
        } else if (lMinCount != null) {
            if (NumberUtil.string2Long(s.toString()) < lMinCount!!) {
                s.replace(0, s.length, "$lMinCount")
            }
        } else if (iMinCount != null) {
            if (NumberUtil.string2Int(s.toString()) < iMinCount!!) {
                s.replace(0, s.length, "$iMinCount")
            }
        } else if (sMinCount != null) {
            if (NumberUtil.string2Short(s.toString()) < sMinCount!!) {
                s.replace(0, s.length, "$sMinCount")
            }
        }
        afterFilter(s)
    }

    open fun afterFilter(s: Editable) {

    }
}